package com.github;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDate;
import java.time.temporal.IsoFields;

/**
 * @author yunnet
 */
public class VersionUtils {
    /**
     * 主版本号
     */
    private final static String MAJOR_VERSION = "2";

    /**
     * 次版本号
     */
    private final static String MINOR_VERSION = "0";

    private final static String VERSION_FILE = "version.txt";

    /**
     * 周期
     */
    public static String getCycleVersion() {
        LocalDate now = LocalDate.now();
        int year = now.getYear() % 100;
        int week = now.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR);
        return String.format("%02d%02d", year, week);
    }

    /**
     * 提交次数
     */
    public static int getGitCommitCount() {
        try {
            FileRepositoryBuilder builder = new FileRepositoryBuilder();
            Repository repository = builder.setGitDir(new File(".git"))
                    .readEnvironment()
                    .findGitDir()
                    .build();
            try (RevWalk revWalk = new RevWalk(repository)) {
                RevCommit headCommit = revWalk.parseCommit(repository.resolve("HEAD"));
                revWalk.markStart(headCommit);

                int count = 0;
                for (RevCommit commit : revWalk) {
                    count++;
                }
                return count;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    public static String writeVersionToFile(String version) {
        try (FileWriter writer = new FileWriter(VERSION_FILE)) {
            writer.write(version);
            File file = new File(VERSION_FILE);
            return file.getAbsolutePath();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String readVersionFromFile() {
        try (FileReader reader = new FileReader(VERSION_FILE)) {
            char[] buffer = new char[50];
            int length = reader.read(buffer);
            if(length > 0) {
                return new String(buffer, 0, length).trim();
            }else{
                return null;
            }
        } catch (IOException e) {
            return null;
        }
    }

    /**
     * 显示版本号
     */
    public static String getVersion() {
        String version = readVersionFromFile();
        if (StringUtils.isNotBlank(version)) {
            return version;
        }
        version = MAJOR_VERSION + "." + MINOR_VERSION + "." + getCycleVersion() + "." + getGitCommitCount();
        writeVersionToFile(version);
        return version;
    }

    public static void main(String[] args) {
        System.out.println("Version: " + getVersion());
    }
}
